package com.hulk.dryad.web.oauth2.basic.social;

import com.hulk.dryad.web.oauth2.basic.service.DryadUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author lqx
 * @date 2018/8/5 手机号登录配置入口
 */
public class SocialSecurityConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {


	@Autowired
	private  DryadUserDetailsService dryadUserDetailsServiceImpl;
	@Autowired
	private AuthenticationSuccessHandler dryadLoginSuccessHandler;
	@Autowired
	private  AuthenticationEntryPoint dryadAuthExceptionEntryPoint;

	@Override
	public void configure(HttpSecurity http) {
		SocialAuthenticationFilter socialAuthenticationFilter = new SocialAuthenticationFilter();
		socialAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
		socialAuthenticationFilter.setAuthenticationSuccessHandler(dryadLoginSuccessHandler);

		socialAuthenticationFilter.setDryadAuthExceptionEntryPoint(dryadAuthExceptionEntryPoint);

		SocialAuthenticationProvider socialAuthenticationProvider = new SocialAuthenticationProvider();
		socialAuthenticationProvider.setUserDetailsService(dryadUserDetailsServiceImpl);
		http.authenticationProvider(socialAuthenticationProvider).addFilterAfter(socialAuthenticationFilter,
				UsernamePasswordAuthenticationFilter.class);
	}

}
